<?php
#############################################################################
# Parse functions for http://etvnet.com site.                               #
#                                                                           #
# Author: consros 2010                                                      #
#############################################################################

require_once "pageTools.inc";
require_once "settings.inc";
require_once "tools.inc";
require_once "etvFunctions.inc";
require_once "parsers.inc";

function displayLinkStyles() {
?>
<style type="text/css">
    table.alphabet { width: <?php echo PAGE_WIDTH?>px; background-color: #4d6080; }
    table.contents { width: <?php echo PAGE_WIDTH?>px; }
    td.section     { width: <?php echo (PAGE_WIDTH/4)?>px; }
    table.links    { width: <?php echo PAGE_WIDTH?>px; }
    td.time        { width: 180px; background-color: #4d6080; }
    td.name        { font-size: 16pt; font-weight: bold; background-color: #005B95; }
    td.stars       { width:  85px; }
    td.rating      { width:  35px; font-size: 11pt; }
    td.length      { width: 120px; background-color: #6d80a0; }
    td.channel     { width: 120px; font-size: 11pt; background-color: #4d6080; }
      .type        { color: #AAAAAA; font-size: 11pt; }
</style>
<?php
}

function displayContents($html) {
    $contents = parseContents($html);
    print "<table class=\"contents\"><tr>\n";
    $i = 0;
    foreach ($contents as $name => $link) {
        # catalog and search are already included on top
        if ('/catalog/' == $link || '/search/' == $link) {
            continue;
        }
        if ($i++ == 4) {
            $i = 1;
            print "</tr><tr>\n";
        }
        $link = $_SERVER['PHP_SELF'] . '?path=' . urlencode($link);
        $link = "<a href=\"$link\">".checkLink($name).'</a>';
        print "<td class=\"section\">$link</td>\n";
    }
    print "</tr></table>\n";
}

function displayAlphabet($path) {
    $linkBase  = $_SERVER['PHP_SELF'] . '?path=' . urlencode($path) . '&opts=';
    $linkBase .= urlencode('sort=name&dir=asc&first_letter=');

    print "<table class=\"alphabet\"><tr><td align=\"center\">\n";
    print "<a href=\"$linkBase" . "EN\">A-Z</a>&nbsp;\n";
    print "<a href=\"$linkBase" . "NUM\">0-9</a>&nbsp;\n";
    for ($i = 0; $i < 32; $i++) {
        # not all letters should be included
        if ($i == 9 || $i == 26 || $i == 27 || $i == 28) {
            continue;
        }
        $c = chr(208).chr(144 + $i);
        $link = $linkBase . urlencode(urlencode($c));
        print "<a href=\"$link\">$c</a>&nbsp;\n";
    }
    print "</td></tr></table>\n";
}

function displaySingleLink($entry) {
    print "<tr>\n";
    print '<td class="time" align="center">'   . $entry->time   . "</td>\n";

    $link = $_SERVER['PHP_SELF'] . '?path=' . urlencode($entry->link);
    print "<td class=\"name\"><a href=\"$link\">" . $entry->name . "</a>";
    if (isset($entry->type) && "" != $entry->type) {
        print "&nbsp;<font class=\"type\"> " . $entry->type;
        if (isset($entry->year) && "" != $entry->year) {
            print ", " . $entry->year;
        }
        print "</font>\n";
    }
    print "</td>\n";

    if (isset($entry->rating) && "" != $entry->rating) {
        print '<td class="rating" align="right">' . $entry->rating . "</td>\n";
    }

    print '<td class="stars" align="center">';
    print "<img src=\"img/stars1/$entry->stars.png\" title=\"$entry->people\" />";
    print "</td>\n";

    if (isset($entry->length) && "" != $entry->length) {
        print '<td class="length" align="right">' . $entry->length . "</td>\n";
    }

    if (isset($entry->channelName)) {
        $link  = $_SERVER['PHP_SELF'] . '?path=';
        $link .= urlencode($entry->channelLink);
        $name = "<a href=\"$link\" class=\"channel\">".checkLink($entry->channelName)."</a>";
        print '<td class="channel">' . $name . "</td>\n";
    }
    print "</tr>\n\n";
}

function displayLinks($html) {
    $linkEntries = parseLinks($html);
    print "<table class=\"links\">\n";
    foreach ($linkEntries as $entry) {
        displaySingleLink($entry);
    }
    print "</table>\n";
}

function displayLinksPage($html, $path) {
    if (hasAlphabet($html)) {
        displayAlphabet($path);
    }
    displayContents($html);
    displayLinks($html);
}

?>
